const App = getApp();
import {
	url
} from '../../modelApi/url_config.js'
Page({
	data: {
		placeholder: "",
		liveIndex: 0,
		showNavBar: !0,
		liveList: [], //直播间列表
		hasNextPage: true,
		liveRoomsParams: {
			// "isRecommend": 0,
			"pageNo": 1,
			"pageSize": 50
		},
		liveStatus: {
			"0":"未开播",
			"1":"直播中",
			"2":"等待开播",
			"3":"暂停",
			"4":"已结束",
			"5":"回放",
		}
	},
	onLoad() {
		if (!App.globalData.isShow) {
			wx.redirectTo({
				url: "/pages/errorPage/index"
			});
			return
		}
	},
	onShow() {
		!wx.getStorageSync("tel") && wx.redirectTo({
			url: "/pages/register/index"
		});
		let liveRoomsParams = {
			// "isRecommend": 0,
			"pageNo": 1,
			"pageSize": 50
		};
		this.setData({
			showNavBar: !0,
			liveList: [],
			liveIndex: 0,
			liveRoomsParams: liveRoomsParams,
			hasNextPage: true
		});
		/* 获取直播间列表 */
		this.getLiveRoomList();

	},
	onPageScroll: function (a) {
		a.scrollTop > 50 ? this.setData({
			showNavBar: !1
		}) : this.setData({
			showNavBar: !0
		});
	},
	switchNav: function (t) {
		var e = t.target.dataset.liveIndex;
		this.setData({
			liveIndex: e,
			hasNextPage: true,
			liveList: []
		});
		this.data.liveRoomsParams.pageNo = 1; //重置页码
		e==="0" ? this.getLiveRoomList() : this.getFocusList() //切换获取数据
	},
	/* 获取直播间列表 */
	getLiveRoomList: function () {
		if (this.data.hasNextPage) {
			wx.showLoading({
				title: "加载中..."
			});
			App.ajax(url.liveRoomListUrl, this.data.liveRoomsParams, 'get').then(res => {
				wx.hideLoading();
				res.rows.forEach(element => {
					element.statusText = this.data.liveStatus[element.status]
				});
				this.data.liveList = [...this.data.liveList, ...res.rows];
				this.setData({
					liveList: this.data.liveList,
					hasNextPage: res.hasNextPage
				})
			})
		}
	},
	/* 获取关注列表 */
	getFocusList: function () {
		if (this.data.hasNextPage) {
			wx.showLoading({
				title: "加载中..."
			});
			let focusParams = {
				isFocus: 1
			}
			App.ajax(url.liveRoomListUrl, {...this.data.liveRoomsParams,...focusParams}, 'get').then(res => {
				wx.hideLoading();
				res.rows.forEach(element => {
					element.statusText = this.data.liveStatus[element.status]
				});
				this.data.liveList = [...this.data.liveList, ...res.rows];
				this.setData({
					liveList: this.data.liveList,
					hasNextPage: res.hasNextPage
				})
			})
		}
	},
	loadMore() {
		if (this.data.hasNextPage) {
			this.data.liveRoomsParams.pageNo++
		} else {
			return;
		}
		if (this.data.liveIndex == 0) {
			this.getLiveRoomList();
		} else {
			this.getFocusList();
		}
	},
});